package org.cable.link.strategy;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

/**
 * 自定义标准分表策略(分表)
 * @author hsj
 * @date 2023/10/30 15:05
 */
public class CustomTablePreciseShardingAlgorithm implements PreciseShardingAlgorithm<String> {

    /**
     *
     * @param tableNames 分表时为对应分⽚库中所有分⽚表的集合 tablesNames
     * @param preciseShardingValue
     * @return
     */
    @Override
    public String doSharding(Collection<String> tableNames, PreciseShardingValue<String> preciseShardingValue) {

         //获取逻辑表
        String targetName = tableNames.iterator().next();

        //短链码
        String value = preciseShardingValue.getValue();

        String codeSuffix = value.substring(value.length() - 1);

       //拼接真实表

        return targetName + "_" + codeSuffix;
    }
}
